CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


obj

  ser   :       "FullDuplexSerial"
  xb    :       "xbee"
  

dat
  new_img         file    "xbee_loader_test2.binary"

phrase  byte  "Open, sesame!###",0
        
pub go | i

  ser.start(25,24,0,9600)
  xb.start(26, 27, 9600)
  
  dira[0]~~                     'prog busy led
  dira[10]~                     'prog start switch
  
  repeat

    

    repeat until not ina[10]    'blink led until start switch is pressed
      !outa[0]
      waitcnt(cnt+clkfreq>>4)

    outa[0]~
    waitcnt(cnt + clkfreq>>1)
    
    outa[0]~~ 
    repeat i from 0 to strsize(phrase) - 1
      ser.tx(byte[@phrase + i]|$80) 'send string to put loader object into command mode
     
    ser.tx("#")
    ser.tx($a5)
    ser.tx($5a)

    i := ser.rxtime(1000)
    if i <> "." or i == -1
      next
     
    ser.tx($a0)
    ser.tx($06)
     
     
    repeat i from 0 to ($06a0 - 1) 'transmit program image
      ser.tx(byte[@new_img + i])
      
    i := ser.rxtime(1000)
    if i <> "." or i == -1
      next
       
    ser.tx("#")
    ser.tx("!")
    

    

    
  